/*
 * ADN3080.c
 *
 *  Created on: 2017年7月28日
 *      Author: Case
 */

#include "ADN3080.h"

#include "ADN3080_srom.h"
#include "delay.h"
#include "r_cg_sci.h"
#include "r_cg_rspi.h"
#include "stdio.h"
/**************************************************
 函数：SPI_RW()
 描述：根据SPI协议，写一字节数据，同时读出一字节
 /**************************************************/
uint32_t SPI_RW(uint8_t byte) {
	uint32_t * P_byte;
	uint32_t SPI_data;
	SPI_delay(10);
	*P_byte = (uint32_t) byte;
	while ((RSPI0.SPSR.BIT.IDLNF))
		;
	SPI_delay(10);
	R_RSPI0_Send_Receive(P_byte, 1, &SPI_data);
	SPI_delay(10);
	while ((RSPI0.SPSR.BIT.IDLNF))
		;
	SPI_delay(10);
	return SPI_data;
}

/**************  ************************************
 函数：SPI_Write_Reg()
 描述：写数据value到reg寄存器
 /**************************************************/
uint8_t SPI_Write_Reg(uint8_t reg, uint8_t value) {
	uint8_t status;
	ADN_CS = 0;
	SPI_delay(10);
	status = SPI_RW(reg);
	SPI_delay(50);
	SPI_RW(value);
	SPI_delay(10);
	ADN_CS = 1;
	return status;
}

/**************************************************
 函数：SPI_Read()
 描述：从reg寄存器读一字节
 /**************************************************/
char SPI_Read_Reg(uint8_t reg) {
	char reg_val;
	ADN_CS = 0;
	SPI_delay(10);
	SPI_RW(reg);                // 选择寄存器
	SPI_delay(20);
	reg_val = SPI_RW(0x00);        // 然后从该寄存器读数据
	SPI_delay(10);
	ADN_CS = 1;
	return (reg_val);            // 返回寄存器数据
}

/**************************************************
 函数：SPI_Read_Buf()
 描述：从reg寄存器读出bytes个字节，通常用来读取接收通道数据或接收/发送地址
 /**************************************************/
uint8_t SPI_Read_Buffer(uint8_t reg, uint8_t * pBuf, uint8_t bytes) {
	uint8_t status, i;
	ADN_CS = 0;
	SPI_delay(10);
	status = SPI_RW(reg + 0x00);       // 选择寄存器，同时返回状态字
	for (i = 0; i < bytes; i++)
		pBuf[i] = SPI_RW(0);    // 逐个字节从nRF24L01读出
	ADN_CS = 1;
	SPI_delay(10);
	return (status);             // 返回状态寄存器
}

/**************************************************
 函数：SPI_Write_Buf()
 描述：把pBuf缓存中的数据写入到目标器件，通常用来写入发射通道数据或接收/发送地址
 /**************************************************/
uint8_t SPI_Write_Buffer(uint8_t reg, uint8_t * pBuf, uint8_t bytes) {
	uint8_t status, i;
	ADN_CS = 0;
	SPI_delay(10);
	status = SPI_RW(reg + 0x80);       // 选择寄存器，同时返回状态字
	for (i = 0; i < bytes; i++)
		SPI_RW(pBuf[i]);        // 逐个字节写入目标器件
	ADN_CS = 1;
	SPI_delay(10);
	return (status);             // 返回状态寄存器
}

/****************************************
 * ADN_3080初始化函数
 ****************************************/
void ADN_Init(void) {
	ADNS3080_reset();		//复位ADNS3080
	ADN_NPD = 1;				//上拉NPD，免睡眠
	SPI_delay(10);
//	ADN_Write_srom();		//配置ADNS3080的寄存器
	ADN_Configuration();
}

void ADNS3080_reset(void)  //ADNS3080 复位（高）
{
	ADN_RST = 0;
	SPI_delay(5);
	ADN_RST = 1;
	SPI_delay(5);
	ADN_RST = 0;
	;  //脉冲信号
}

/*******************************************
 * ADN_3080配置寄存器
 *******************************************/
void ADN_Write_srom(void) {
	int i;
//	ADN_CS = 0;
	SPI_Write_Reg(0x20, 0x44);
	User_Delay(10);
	SPI_Write_Reg(0x23, 0x07);
	User_Delay(10);
	SPI_Write_Reg(0x24, 0x88);
	User_Delay(10);
//	ADN_CS = 1;

	SPI_delay(5);

//	ADN_CS = 0;
	SPI_Write_Reg(SROM_Enable, 0x18);
//	ADN_CS = 1;
	SPI_delay(1);

//	ADN_CS = 0;
	for (i = 0; i <= 1985; i++) {
		SPI_Write_Reg(0x60, SROM[i]);
		User_Delay(10);
	}
//	ADN_CS = 1;
	SPI_delay(2);
}

void ADN_Configuration(void) {
//	ADN_CS = 0;
	SPI_Write_Reg(Configuration_bits, 0x10);		//设置分辨率为 1600
	SPI_delay(3);
	SPI_Write_Reg(Extended_Config, 0x01);
	SPI_delay(3);
	if (read_busy() != 1) {
//		ADN_CS = 0;
		SPI_RW(Frame_Period_Max_Bound_Lower + 0x80);
		SPI_RW(0x40);
		SPI_RW(Frame_Period_Min_Bound_Lower + 0x80);
		SPI_RW(0x1f);
//		ADN_CS = 1;
	}
	clear_motion();
//	ADN_CS = 1;
}

uint8_t read_busy(void)		//写帧率的判忙  ==1忙
{
	uint8_t temp;
//	ADN_CS = 0;
	temp = SPI_RW(Extended_Config + 0x00);
	User_Delay(10);
	temp = SPI_RW(0xff);
	temp &= 0x80;
//	ADN_CS = 1;
	return temp;
}

void clear_motion(void) {
//	ADN_CS = 0;
	SPI_RW(Motion_Clear + 0x80);
	SPI_RW(0xff);
//	ADN_CS = 1;
}

uint16_t read_zhenlv(void) {
	uint16_t Frame_Period_Max_Bound_Lower1, Frame_Period_Max_Bound_Upper1;
//	ADN_CS = 0;
	Frame_Period_Max_Bound_Upper1 = SPI_RW(Frame_Period_Uppe + 0x00);
	Frame_Period_Max_Bound_Upper1 = SPI_RW(0xff);		//接收高位的帧率
	SPI_delay(5);
	Frame_Period_Max_Bound_Lower1 = SPI_RW(Frame_Period_Lower + 0x00);
	Frame_Period_Max_Bound_Lower1 = SPI_RW(0xff); //接收低位的帧率
//	ADN_CS = 1;
	return ((Frame_Period_Max_Bound_Upper1 << 8) | Frame_Period_Max_Bound_Lower1);
}

void Read_Data_burst(int	*L_x,int *L_y)
{
	static int SumX;
	static int SumY;
	char _n = '\n';
	char _t = '\t';
	char sun_x[5];
	char sun_y[5];
	int sum_x, sum_y;
	unsigned char move = 0;
	int x = 0;
	int y = 0;
	char ID, Revision;
	char ADNS_Motion[8] = { 0 };
	char Motion_x;
	char Motion_y;

	SPI_Write_Reg(0x0a, 0x50);
	SPI_Write_Reg(0x12, 0x12);
	SPI_delay(200);
	Burst_ADN3080(0x50, ADNS_Motion);
	SPI_delay(50);

	SPI_delay(50);
	if (ADNS_Motion[0] & 0x80) {
		x = covter_int(ADNS_Motion[1]);
		y = covter_int(ADNS_Motion[2]);
	}
	SumX = SumX + x;             //累加X读入的移动数据
	SumY = SumY + y;			 //累加Y读入的移动数据
	sum_x = (25.4 * (float) SumX * H) / (12 * 1600);//距离=d_x*(25.4/1600)*n   其中n=像高:物高=8毫米:物长
	sum_y = (25.4 * (float) SumY * H) / (12 * 1600);
	if (ADNS_Motion[0] & 0x80)
	{
		*L_x = sum_x;
		*L_y = sum_y;
//		sun_x[0] = sum_x / 1000 + 48;
//		sun_x[1] = sum_x % 1000 / 100 + 48;
//		sun_x[2] = sum_x % 100 / 10 + 48;
//		sun_x[3] = sum_x % 10 + 48;
//
//		sun_y[0] = sum_y / 1000 + 48;
//		sun_y[1] = sum_y % 1000 / 100 + 48;
//		sun_y[2] = sum_y % 100 / 10 + 48;
//		sun_y[3] = sum_y % 10 + 48;
//
//		R_SCI1_Serial_Send_Data(sun_x, 5);
//		R_SCI1_Serial_Send_Data(&_t, 1);
//		R_SCI1_Serial_Send_Data(sun_y, 5);
//		R_SCI1_Serial_Send_Data(&_n, 1);
	} else {
		x = 0;
		y = 0;
	}
}

float read_average_pixel(void)	  //读平均像素
{
	float temp;
//	ADN_CS = 0;
	temp = SPI_RW(Pixel_Sum);
	User_Delay(20);
	temp = SPI_RW(0xff);
	temp = temp * 256 / 900;
	ADN_CS = 1;
	return temp;
}

void read_pixel(void) {
	uint8_t i, j, regValue, pixelValue, test = 1;
	SPI_Write_Reg(Frame_Capture, 0x83);
	SPI_delay(1);	  //等待3帧 (1/3000)*1000000*3+10 =1010us
	//显示数据  30*30=900
	for (i = 0; i < 30; i++)  //列
			{
		for (j = 0; j < 30; j++) //行
				{
			regValue = SPI_Read_Reg(Frame_Capture);  //读像素
			if (test && ((regValue & 0x40) == 0)) //找不到第一个像素
					{

			}
			test = 0;
			pixelValue = (regValue << 2);
			User_Delay(10);
		}
	}
	ADNS3080_reset(); //重启运行
}

void read_pixel_burst(void) //爆发读图像
{
	int i, j;
	SPI_Write_Reg(Frame_Capture, 0x83);
	SPI_delay(1); //等待3帧 (1/3000)*1000000*3+10 =1010us
//开始burst读
	ADN_CS = 0;
	SPI_RW(0x40);
	User_Delay(10);
	for (i = 0; i < 30; i++) {
		for (j = 0; j < 30; j++) {
//			(SPI_RW(0xFF) << 2);
		}
	}
	ADN_CS = 1;
	User_Delay(1);
	ADN_CS = 1;
}

void Burst_ADN3080(uint8_t reg, uint8_t *data) {
	uint8_t i;
	ADN_CS = 0;
	SPI_delay(10);
	SPI_RW(reg);
	SPI_delay(50);
	for (i = 0; i < 7; i++) {
		data[i] = SPI_RW(0xff);
		SPI_delay(10);
	}
	ADN_CS = 1;
}

int covter_int(char num) {
	int temp;
	if (num < 0x80) {
		temp = num;
	} else {

		temp = -(128 - (num & 0x7F));
	}
	return temp;
}
